<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core\lang;
use classlib\core as C;
use classlib\core\controller;
class Csalary extends controller {
	public function main() {
		$view  = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$dlang = C\lang::inst('dd');
		$view->assign('status', $dlang->get('sh0005'));
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('live/salary.ext.js');
	}
	
	//执行流程信息跳转或者创建流程
	public function dotask() {
		$id = (int)input::get_post('s');
		if ($id < 1) {
			C\redirect('/admin');
		}
		$orm  = C\model::load('pro.pl.salary');
		$data = $orm->get_one($id);
		if (!$data) {
			C\redirect('/admin');
		}
		if ($data['executeid'] > 0) {
			$url = Cadmin::taskurl($data['executeid']);
			C\redirect($url);
		}
		//已经经理了一办的审核或者经过审核了 就不允许了
		if ($data['status'] != 0 && $data['state'] != 0) {
			C\redirect('/admin');
		}
		$uorm = C\model::load('pro.pl.userinfo');
		$user = $uorm->get_one($data['userid']);
		$star = json_decode($data['star'], true);
		$star = $star['name'].'['.$star['star'].']';
		$user = $user['nickname'].'['.$user['email'].']';
		$sdate= date('Y-m-d H:i:s', $data['starttime']);
		$etime= date('Y-m-d H:i:s', $data['endtime']);
		$atime= date('Y-m-d H:i:s', $data['addtime']);
		$args = array('userid'=>$data['userid'], 'status'=>1, 'sdate'=>$sdate, 'edate'=>$etime);
		$args = \classlib\request::querystr($args);
		$link = C\router::get_url('admin/live/basic/event').'?'.$args;
		//创建流程执行实例 进行审核
		$item = array('id'=>$data['id'], 'nickname'=>$user, 'starlv'=>$star, 'link'=>$link, 'starttime'=>$sdate, 
			'qualifiedhours'=>$data['qualifiedhours'], 'unqualifiedhours'=>$data['unqualifiedhours'], 
			'endtime'=>$etime, 'bonus'=>$data['bonus'], 'salary'=>$data['salary'], 'addtime'=>$atime
		);
		$json = json_encode(array('salary'=>array($item)));
		$executeid = Cadmin::build_execute(Cadmin::WORKFLOW_SALARY_ID, '0', $json);
		$orm->save($id, array('executeid'=>$executeid));
		$url = Cadmin::taskurl($executeid);
		C\redirect($url);
	}
	
	public function dolist() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$where  = array();
		$state  = input::post('state');
		is_numeric($state) && $where['state'] = $state;
		$status = input::post('status');
		is_numeric($status) && $where['status'] = $status;
		$userid = input::post('userid');
		is_numeric($userid) && $where['userid'] = $userid;
		$sdate  = input::post('sdate');
		if ($sdate) {
			$where[] = array('addtime', strtotime($sdate), \classlib\orm::OP_GT);
		}
		$edate  = input::post('edate');
		if ($edate) {
			$where[] = array('addtime', strtotime($edate), \classlib\orm::OP_LT);
		}
		$orm    = C\model::load('pro.pl.salary');
		$total  = $orm->get_total($where);
		$list   = array();
		if ($total > 0) {
			$list = $orm->get_list($start, $limit, $where, '*', $sort);
			$uorm = C\model::load('pro.pl.userinfo');
			foreach ($list as &$item) {
				$user = $uorm->get_one($item['userid']);
				$item['starttime'] = date('Y-m-d H:i:s', $item['starttime']);
				$item['endtime']   = date('Y-m-d H:i:s', $item['endtime']);
				$item['audittime'] = ($item['audittime'] > 0)? date('Y-m-d H:i:s', $item['addtime']):'';
				$item['addtime']   = date('Y-m-d H:i:s', $item['addtime']);
				$item['stime']     = date('Y-m-d H:i:s', $item['stime']);
				$item['nickname']  = $user['nickname'].'['.$user['email'].']-'.$user['userid'];
				if ($item['executeid'] > 0) {
					$item['link']  = Cadmin::taskurl($item['executeid']);
				} else {
					$item['link']  = C\router::get_url('admin/live/finance/salary', 'dotask', array('s'=>$item['id']));
				}
				$args = array('userid'=>$item['userid'], 'status'=>1, 'sdate'=>$item['starttime'], 'edate'=>$item['endtime']);
				$args = \classlib\request::querystr($args);
				$item['details'] = C\router::get_url('admin/live/basic/event').'?'.$args;
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
}